﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using DAL.Entiteti;
using MySql.Data.MySqlClient;

namespace DAL
{
    partial class DAL
    {
        public class IgracDAO : IDaoCrud<Igrac>
        {
            #region IDaoCrud<Clan> Members
            protected MySqlCommand c,c1;

            public long create(Igrac entity)
            {
                try
                {
                    c = new MySqlCommand("INSERT INTO Igraci  (Ime_Igraca,Prezime_Igraca,Dan_Rodjenja,Grad_Rodjenja,JMBG,Broj_Poena,Slika_Igraca,idKluba) VALUES ('" + entity.Ime + "','" + entity.Prezime + "','" + entity.DajDan + "','" + entity.DajGrad + "','" + entity.DajJMBG + "','" + entity.DajPoene + "','" + entity.Slika + "','"+entity.DajIDKluba+"')", con);
                    c.ExecuteNonQuery();
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Igrac> getAll()
            {
                c = new MySqlCommand("select * from igraci", con);
                MySqlDataReader r = c.ExecuteReader();
                try
                {
                   
                    List<Igrac> clanovi = new List<Igrac>();
                    while (r.Read())
                    {
                        string temp = r.GetString("Dan_Rodjenja");
                        Igrac f = new Igrac(r.GetString("Ime_Igraca"), r.GetString("Prezime_Igraca"), r.GetString("Dan_Rodjenja"), r.GetString("Slika_igraca"), r.GetString("Grad_Rodjenja"), r.GetString("JMBG"));
                        f.DajIDIgraca=r.GetInt32("idIgraci");
                        f.DajIDKluba = r.GetInt32("idKluba");
                        f.Slika = r.GetString("Slika_igraca");
                        f.DajPoene = r.GetInt32("Broj_Poena");
                        clanovi.Add(f);
                    }
                    r.Close();
                    return clanovi;
                }
                catch (Exception e)
                {
                    r.Close();
                    throw e;
                }
            }


            public Igrac read(Igrac entity)
            {
                throw new NotImplementedException();
            }

            public Igrac update(Igrac entity)
            {
                c1 = new MySqlCommand("UPDATE igraci SET Ime_Igraca= '" + entity.Ime + "',Prezime_Igraca='" + entity.Prezime + "',Dan_Rodjenja='"+entity.DajDan+"' ,Grad_Rodjenja='"+entity.DajGrad+"'"+", JMBG= '"+entity.DajJMBG+"', Broj_Poena="+entity.DajPoene+",Slika_igraca='"+entity.Slika+"' WHERE  idIgraci="+entity.DajIDIgraca, con);
                c1.ExecuteNonQuery();
                return entity;
            }

            public void delete(Igrac entity)
            {
                c1 = new MySqlCommand("DELETE FROM igraci WHERE idIgraca= " + entity.DajIDIgraca, con);
                c1.ExecuteNonQuery();
            }

            public Igrac getById(double id)
            {
                c = new MySqlCommand("SELECT * from clan WHERE idIgraci=" + id, con);
                MySqlDataReader r = c.ExecuteReader();
                try { 
                r.Read();
                Igrac cla = new Igrac(r.GetString("Ime_Igraca"), r.GetString("Prezime_Igraca"), r.GetString("Dan_Rodjenja"), r.GetString("Slika_igraca"), r.GetString("Grad_Rodjenja"), r.GetString("JMBG"));
                cla.DajIDIgraca = r.GetInt32("IDClana");
                cla.DajIDKluba = r.GetInt32("idKluba");
                cla.Slika = r.GetString("Slika_igraca");
                r.Close();
                return cla;
                    }
                catch (Exception t)
                {
                    r.Close();
                    throw t;
                }       
            }

            public List<Igrac> getByExample(string name)
            {
                c = new MySqlCommand("SELECT * FROM Igraci WHERE Ime_Igraca like '%" + name + "%' OR Prezime_Igraca like '%"+ name +"%'", con);
                MySqlDataReader r = c.ExecuteReader();
                List<Igrac> lista_i = new List<Igrac>();
                try
                {
                    while (r.Read())
                    {
                        Igrac cla = new Igrac(r.GetString("Ime_Igraca"), r.GetString("Prezime_Igraca"), r.GetString("Dan_Rodjenja"), r.GetString("Slika_igraca"), r.GetString("Grad_Rodjenja"), r.GetString("JMBG"));
                        cla.DajIDIgraca = r.GetInt32("idIgraci");
                        cla.Slika = r.GetString("Slika_igraca");
                        cla.DajIDKluba = r.GetInt32("idKluba");
                        lista_i.Add(cla);
                    }
                    r.Close();
                    return lista_i;
                }
                catch (Exception t)
                {
                    r.Close();
                    throw t;
                }
            }
            public void deleteCon(double id)
            {
                c1 = new MySqlCommand("DELETE klubovi WHERE idKluba= "+id, con);
                c1.ExecuteNonQuery();
            }
            public List<Igrac> GetByClubID(double id)
            {
                c = new MySqlCommand("SELECT * FROM Igraci WHERE idKluba =" + id, con);
                MySqlDataReader r = c.ExecuteReader();
                List<Igrac> lista_i = new List<Igrac>();
                try
                {
                    while (r.Read())
                    {
                        Igrac cla =  new Igrac(r.GetString("Ime_Igraca"), r.GetString("Prezime_Igraca"), r.GetString("Dan_Rodjenja"), r.GetString("Slika_igraca"), r.GetString("Grad_Rodjenja"), r.GetString("JMBG"));
                        cla.DajIDIgraca = r.GetInt32("IDIgraci");
                        cla.Slika = r.GetString("Slika_igraca");
                        lista_i.Add(cla);
                    }
                    r.Close();
                    return lista_i;

                }
                catch (Exception t)
                {
                    r.Close();
                    throw t;
                }
            }
            #endregion
        }
    }
}
